<template>
	<view class="address-manage rf-row-wrapper">
	  <block v-if="manageType === '电话'">
		<view class="row b-b">
			<text class="tit">联系人一</text>
			<input
				class="input"
				type="text"
				v-model="watchModel.name1"
				placeholder="联系人一"
				placeholder-class="placeholder"
			/>
		</view>
		<view class="row b-b">
			<text class="tit">电话号码</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.tel1"
				placeholder="电话号码"
				placeholder-class="placeholder"
			/>
		</view>

		<view class="row b-b">
			<text class="tit">联系人二</text>
			<input
				class="input"
				type="text"
				v-model="watchModel.name2"
				placeholder="联系人二"
				placeholder-class="placeholder"
			/>
		</view>
		<view class="row b-b">
			<text class="tit">电话号码</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.tel2"
				placeholder="电话号码"
				placeholder-class="placeholder"
			/>
		</view>
		
		<view class="row b-b">
			<text class="tit">联系人三</text>
			<input
				class="input"
				type="text"
				v-model="watchModel.name3"
				placeholder="联系人三"
				placeholder-class="placeholder"
			/>
		</view>
		<view class="row b-b">
			<text class="tit">电话号码</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.tel3"
				placeholder="电话号码"
				placeholder-class="placeholder"
			/>
		</view>		
		
		<view class="row b-b">
			<text class="tit">联系人四</text>
			<input
				class="input"
				type="text"
				v-model="watchModel.name4"
				placeholder="联系人四"
				placeholder-class="placeholder"
			/>
		</view>
		<view class="row b-b">
			<text class="tit">电话号码</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.tel4"
				placeholder="电话号码"
				placeholder-class="placeholder"
			/>
		</view>	
	</block>	
					
	<block v-else-if="manageType === 'SOS'">
		<view class="row b-b">
			<text class="tit">SOS号码</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.sos"
				placeholder="SOS号码"
				placeholder-class="placeholder"
			/>
		</view>		
		
	</block>					

	  <block v-else-if="manageType === '定位'">
		<view class="row b-b">
			<text class="tit">定位间隔</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.posInterval"
				placeholder="定位间隔"
				placeholder-class="placeholder"
			/>
		</view>
		<text  class="tips" :class="'text-' + themeColor.name">
				设置区间：3-600 单位（分钟）
		</text>		
		<view class="row b-b">
			<text class="tit">心率间隔</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.heartInterval"
				placeholder="心率间隔"
				placeholder-class="placeholder"
			/>
		</view>
		<text  class="tips" :class="'text-' + themeColor.name">
				设置区间：30-600 单位（分钟）
		</text>		
		<view class="row b-b">
			<text class="tit">步数间隔</text>
			<input
				class="input"
				type="number"
				v-model="watchModel.stepInterval"
				placeholder="步数间隔"
				placeholder-class="placeholder"
			/>
		</view>
		<text  class="tips" :class="'text-' + themeColor.name">
				设置区间：30-600 单位（分钟）
		</text>		
	</block>
					
					

		<button
			class="add-btn"
			:class="'bg-' + themeColor.name"
			:disabled="btnLoading"
			:loading="btnLoading"
			@tap="confirm"
		>
			提交
		</button>

		<!--加载动画-->
		<rfLoading isFullScreen :active="loading"></rfLoading>
	</view>
</template>

<script>
/**
 * @des 收货地址列表
 *
 * @author stav stavyan@qq.com
 * @date 2020-03-10 18:00
 * @copyright 2019
 */
import { getHuInfo, addAllPhone,speciallyPhoneSet,locationMode} from '@/api/userInfo';
import rfPickRegions from '@/components/rf-pick-regions';
export default {
	components: {
		rfPickRegions
	},
	data() {
		return {
			watchModel: {
				cstmId : '',
				name1 : '',
				tel1 : 	'',
				name2: '',
				tel2: '',
				name3: '',
				tel3: '',
				name4: '',
				tel4: '',
				heartInterval : '',
				stepInterval : '',
				posInterval : '',
				sos : 	''	

			},
			manageType : '',
			btnLoading: false,
			multiArray: [[], [], []],
			multiIndex: [0, 0, 0],
			loading: true,
			userInfo : {}
		};
	},
	onLoad(options) {
		this.initData(options);
	},
	methods: {
		// 获取选择的地区
		handleGetRegions(e) {
			this.addressData.provinceIds = e.provinceIds;
			this.addressData.cityIds = e.cityIds;
			this.addressData.countyIds = e.countyIds;
		},
		// 数据初始化
		async initData(options) {
			//this.userInfo = uni.getStorageSync('userInfo');
			//this.addressData.cstmId = this.userInfo && this.userInfo.userId;
			//this.addressData.cstmName = this.userInfo && this.userInfo.userName;
			//this.addressData.corpId = this.userInfo && this.userInfo.corpId;
			this.manageType = options.type;
						
			let title = '电话本设置';
			if (this.manageType.type === 'SOS') {
				title = 'SOS号码设置';
			} else if(this.manageType.type === '定位')  {ss
				title = '腕表上传数据频率设置';
			}

			await this.getAddressDetail(options.id);			
			uni.setNavigationBarTitle({
				title
			});
		},
		// 获取收货地址
		async getAddressDetail(id) {
			var params = {
               cstmId: id
          }
			await this.$http
				.get(`${getHuInfo}`, params)
				.then(async r => {
					if(r.data.success){
						console.log("r.data: " + JSON.stringify(r.data));
						this.watchModel = await r.data.obj;	
						this.watchModel.cstmId = id;
						//console.log("this.watchModel: " + JSON.stringify(this.watchModel));					
					}else{
						this.$mHelper.toast(r.data.msg);
					}

					
					this.loading = false;
				})
				.catch(() => {
					this.loading = false;
				});
		},
		handleRealNameChange(e) {
			this.addressData.linkMan = e.detail.value;
		},
		bindAddressDetailsChange(e) {
			this.addressData.homeAddress = e.detail.value;
		},
		handleMobileChange(e) {
			this.addressData.linkTel = e.detail.value;
		},
		switchChange(e) {
			this.addressData.addressState = e.detail.value ? 2 : 1;
		},
		// 提交
		confirm() {
			let data = this.watchModel;			  
			// if (!data.linkMan) {
			// 	this.$mHelper.toast('请填写收货人姓名');
			// 	return;
			// }
			if(this.manageType === '电话'){
				if (data.tel1 && !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(data.tel1)) {
					this.$mHelper.toast('联系电话一格式不正确，请重新填写');
					return;
				}
				if (data.tel2 && !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(data.tel2)) {
					this.$mHelper.toast('联系电话二格式不正确，请重新填写');
					return;
				}			
				if (data.tel3 && !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(data.tel3)) {
					this.$mHelper.toast('联系电话三格式不正确，请重新填写');
					return;
				}	
				if (data.tel4 && !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(data.tel4)) {
					this.$mHelper.toast('联系电话四格式不正确，请重新填写');
					return;
				}				
			}else if(this.manageType === 'SOS'){
				if (data.sos && !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(data.sos)) {
					this.$mHelper.toast('sos号码格式不正确，请重新填写');
					return;
				}				
			}
							
						
			this.btnLoading = true;
			// if (this.manageType === 'edit') {
			// 	this.handleAddressUpdate(data);
			// } else {
			// 	this.handleAddressCreate(data);
			// }
			//console.log("data: " + JSON.stringify(data));
			
			this.handleAddressUpdate(data);
			
			
		},
		async handleAddressUpdate(data) {						
			let url = `${addAllPhone}`;
			if(this.manageType === 'SOS'){
				url = `${speciallyPhoneSet}`;
				data.method = 2;				 
			}else if(this.manageType === '定位'){
				data.method = 1;
				url = `${locationMode}`;
			}  
			// console.log("请求参数: " + JSON.stringify(data));
			await this.$http
				.get(url, data)
				.then(r => {
					console.log("保存返回: " + JSON.stringify(r.data));
					if(r.data.success){
						this.btnLoading = false;
						this.$mHelper.toast('设置成功！');
						this.$mRouter.back();						
					}else{
						this.btnLoading = false;
						this.$mHelper.toast(r.data.msg);
					}

				})
				.catch(() => {
					this.btnLoading = false;
				});
		},
		async handleAddressCreate(data) {
			data.editType = 1;
			await this.$http
				.get(`${addressCreate}`, {
					cstmId : data.cstmId,
					cstmName : data.cstmName,
					corpId : data.corpId,
					linkMan: data.linkMan,
					linkTel: data.linkTel,
					homeAddress: data.homeAddress,
					addressState: data.addressState ,
					provId: data.provinceIds,
					cityId: data.cityIds,
					countyId: data.countyIds,
					editType : data.editType
					
				})
				.then(r => {
					if(r.data.success){
						this.btnLoading = false;
						this.$mHelper.toast('收货地址修改成功！');
						this.$mRouter.back();
					}else{
						this.btnLoading = false;
						this.$mHelper.toast(r.data.msg);
					}

				})
				.catch(() => {
					this.btnLoading = false;
				});
		}
	}
};
</script>

<style lang="scss">
page {
	background: $page-color-base;
}
.address-manage {
	padding-top: 16upx;
}
</style>
